// pages/login/login.js
var cookie = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    password: '',
    isLogin: false
  },
  //获取用户名
  userName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  //获取密码
  userPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  showErrorToastUtils: function (e) {
    wx.showModal({
      title: '提示！',
      confirmText: '确定',
      showCancel: false,
      content: e,
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
        }
      }
    })
  },
  checkInput: function () {
    if (this.data.userName == "" || this.data.userName == null ||
      this.data.userName == undefined) {
      this.showErrorToastUtils('请输入用户名');
    } else if (this.data.password == "" || this.data.password == null || this.data.password == undefined) {
      this.showErrorToastUtils('请输入密码');
    } else if (this.data.password.length < 3) {
      this.showErrorToastUtils('密码至少要3位');
    }
    return true;
  },
  login: function () {
    var that = this;
    var isrightfun = that.checkInput();
    console.log(isrightfun)
    if (isrightfun) {
      wx.request({
        url: 'http://39.98.173.131:7000/wlhse/api/v3/login',
        method: "POST",
        data: {
          userName: that.data.userName,
          psw: that.data.password,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          // cookie = res.cookies
          console.log(res)
          if (res.data.code == 1000) {
            console.log("登录成功啦！")
            console.log(res)
            // console.log(JSON.stringify(res.header))
            // wx.setStorageSync("token", res.header["Set-Cookie"])
            // console.log(JSON.stringify(wx.getStorageSync("token")))
            wx.setStorageSync("token", res.data.data.token)
            console.log(JSON.stringify(wx.getStorageSync("token")))
            wx.switchTab({
              url: '../index/index',
            })
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            console.log("登录失败啦！")
            if (that.data.userName.length > 0 && that.data.password.length > 0) {
              wx.showToast({
                title: '账号或密码错误',
                icon: 'none',
                duration: 2000
              })
            }
          }

        },
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})